package com.bj58.migrate.update.impl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.bj58.migrate.ScfProxy;
import com.bj58.migrate.bll.CMCSBll;
import com.bj58.migrate.model.AvgSalaryModel;
import com.bj58.migrate.update.DataHandler;
import com.bj58.sfft.cmc.entity.Category;
import com.bj58.sfft.cmc.entity.Local;

/**
 * Created by lvzheng on 2014/8/8.
 */
public class AvgCrawlHandleImpl implements DataHandler{

	private Logger logger = Logger.getLogger( AvgCrawlHandleImpl.class );
	private static final String[] salaryFilter = { "&source=0|6", "&param5354=0_999&source=0|6", "&param5354=1000_1999&source=0|6",
			"&param5354=2000_2999&source=0|6", "&param5354=3000_4999&source=0|6", "&param5354=5000_7999&source=0|6",
			"&param5354=8000_11999&source=0|6", "&param5354=12000_19999&source=0|6", "&param5354=20000_24999&source=0|6",
			"&param5354=25000_999999&source=0|6" };

	public void handle( String condition, String[] args ){

		logger.info( "Handling.... " + args[ 2 ] + " " + condition );

		AvgSalaryModel business = new AvgSalaryModel();

		try{
			ConditonParser.parseAndSet( condition, business );
		}
		catch( Exception ex ){
			logger.error( "ConditonParser.parseAndSet:" + condition, ex );
		}
		try{
			int infocount = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 0 ] );
			int countless_1000 = 0, count1000_2000 = 0, count2000_3000 = 0, count3000_5000 = 0, count5000_8000 = 0, count8000_12000 = 0, count12000_20000 =
					0, count20000_25000 = 0, countthan_25000 = 0, count_mianyi = 0;

			if( infocount > 0 ){
				countless_1000 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 1 ] );
				count1000_2000 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 2 ] );
				count2000_3000 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 3 ] );
				count3000_5000 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 4 ] );
				count5000_8000 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 5 ] );
				count8000_12000 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 6 ] );
				count12000_20000 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 7 ] );
				count20000_25000 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 8 ] );
				countthan_25000 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 9 ] );
				count_mianyi =
						infocount - countless_1000 - count1000_2000 - count2000_3000 - count3000_5000 - count5000_8000 - count8000_12000
								- count12000_20000 - count20000_25000 - countthan_25000;
			}
			business.setInfocount( infocount );
			business.setCount_mianyi( count_mianyi > 0 ? count_mianyi : 0 );
			business.setCountless_1000( countless_1000 );
			business.setCount1000_2000( count1000_2000 );
			business.setCount2000_3000( count2000_3000 );
			business.setCount3000_5000( count3000_5000 );
			business.setCount5000_8000( count5000_8000 );
			business.setCount8000_12000( count8000_12000 );
			business.setCount12000_20000( count12000_20000 );
			business.setCount20000_25000( count20000_25000 );
			business.setCountthan_25000( countthan_25000 );
			business.caculateAvg();
			business.write();
			business.show();
		}
		catch( Exception e ){
			logger.error( "AvgCrawlHandleImpl get infos Exception:" + condition, e );
			e.printStackTrace();
		}
	}
}

class ConditonParser{

	private static final Pattern cate_pattern = Pattern.compile( "cateid=(\\d+)" );
	private static final Pattern local_pattern = Pattern.compile( "local=(\\d+)" );
	private static final Pattern post_pattern = Pattern.compile( "postdate=(\\d+)_(\\d+)" );

	/**
	 * 分析condition，获取类别，地域信息<br>
	 * 并设置对象相关属性
	 * 
	 * @param condtion
	 * @param model
	 */
	public static void parseAndSet( String condtion, AvgSalaryModel model ){

		Matcher mccate = cate_pattern.matcher( condtion );
		Matcher mclocal = local_pattern.matcher( condtion );
		Matcher mpost = post_pattern.matcher( condtion );

		int level3 = -1;
		int local = -1;
		int start = 0;
		int end = 0;

		if( mccate.find() ){

			level3 = Integer.parseInt( mccate.group( 1 ) );
			Category ext = CMCSBll.GetCatetory( level3 );

			model.setCateid( level3 );
			if( ext != null ){
				model.setCatename( ext.getCateName() );
			}
		}

		if( mclocal.find() ){

			local = Integer.parseInt( mclocal.group( 1 ) );
			Local localext = CMCSBll.GetDispLocal( local );

			if( localext != null ){
				model.setLocalname( localext.getLocalName() );
			}
			model.setLocalid( local );
		}

		if( mpost.find() ){

			start = Integer.parseInt( mpost.group( 1 ) );
			end = Integer.parseInt( mpost.group( 2 ) );

			model.setStartdate( start );
			model.setEnddate( end );
		}
	}

}